/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset


* ---------
* Table A16
* ---------

* Globals
global dep "ln_time fraud c1_dlog_val c1_dlog_tax htrl_f_int"
global controls "tax_rate risk red_i mixed_dec differentiated valitrade_advice"

* Matrix
** Panels with one independent variable of interest
forvalues x = 1(1)6 {
	mat tableA16_`x' = J(4,5,.)
	mat tableA16_`x'T = J(1,5,.)
}


* Panels for confidence
* ---------------------

* Panel A: 99% confidence
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' sig_c_f99 $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

	mat tableA16_1[`i',`j'] = _b[sig_c_f99]
	local i = `i' + 1
	mat tableA16_1[`i',`j'] = _se[sig_c_f99]
	mat tableA16_1T[1,`j'] = e(N)
	local i = `i' + 2
	mat tableA16_1[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}

* Panel B: 95% confidence
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' sig_c_f95 $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

	mat tableA16_2[`i',`j'] = _b[sig_c_f95]
	local i = `i' + 1
	mat tableA16_2[`i',`j'] = _se[sig_c_f95]
	mat tableA16_2T[1,`j'] = e(N)
	local i = `i' + 2
	mat tableA16_2[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}

* Panel A: 99.99% confidence
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' sig_c_f999 $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

	mat tableA16_3[`i',`j'] = _b[sig_c_f999]
	local i = `i' + 1
	mat tableA16_3[`i',`j'] = _se[sig_c_f999]
	mat tableA16_3T[1,`j'] = e(N)
	local i = `i' + 2
	mat tableA16_3[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}


* Panels for minimum quantity of declarations
* -------------------------------------------
local t = 4
foreach val in 20 100 {

	* Loop for estimations
	local i = 1
	local j = 1
	foreach x in $dep {

		qui: reghdfe `x' eis_f $controls countryorig* max_twodigit* if sample_reg_`val'==1 & wfr==. , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

		mat tableA16_`t'[`i',`j'] = _b[eis_f]
		local i = `i' + 1
		mat tableA16_`t'[`i',`j'] = _se[eis_f]
		mat tableA16_`t'T[1,`j'] = e(N)
		local i = `i' + 2
		mat tableA16_`t'[`i',`j'] = e(r2)
		local i = `i' -3
		local j = `j' +1
	}
	
	local t = `t'+1
}


* Panel F: all brokers
* --------------------
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' eis_f $controls countryorig* max_twodigit* if sample_reg_allb==1 & wfr==. , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)

	mat tableA16_6[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat tableA16_6[`i',`j'] = _se[eis_f]
	mat tableA16_6T[1,`j'] = e(N)
	local i = `i' + 2
	mat tableA16_6[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table A16.xlsx", replace sheet(TableA16) // create a new excel

* Title
putexcel (A1:F1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Dependent variables
putexcel A2 = "Dependent variable:", left vcenter bold
local cells `" "B" "C" "D" "E" "F" "'
local labels `" "Time" "Fraud" "\Delta log value" "\Delta log tax" "Hyp. tax revenue losses" "'
forvalues i = 1(1)5 {

	local x: word `i' of `labels'
	local y: word `i' of `cells'

	putexcel `y'2 = "`x'", hcenter vcenter bold	
}

* Panels
** Name
local y = 4
local labels `" "A. Indicator for Significant excess Interaction (99 Percent Confidence)" "B. Indicator for Significant Excess Interaction (95 Percent Confidence)" "C. Indicator for Significant Excess Interaction (99.9 Percent Confidence)" "D. Alternative Sample Including Brokers with More than 20 Declarations per Semester" "E. Alternative Sample Including Brokers with More than 100 Declarations per Semester" "F. Alternative Sample Including All Brokers" "'
forvalues i = 1(1)6 {

	local x: word `i' of `labels'

	putexcel (A`y':F`y'), merge hcenter vcenter bold
	putexcel A`y' = "`x'"
	local y = `y'+7
}
** Coefficients
local j = 1
forvalues i = 6(7)21 {

	putexcel A`i' = "Excess interaction indicator", left vcenter
	putexcel B`i' = matrix(tableA16_`j'), nformat(0.000) hcenter vcenter
	local j = `j'+1
}
local j = 4
forvalues i = 27(7)41 {

	putexcel A`i' = "Excess interaction share", left vcenter
	putexcel B`i' = matrix(tableA16_`j'), nformat(0.000) hcenter vcenter
	local j = `j'+1
}
** Observations
local j = 1
forvalues i = 8(7)43 {

	putexcel A`i' = "Observations", left vcenter
	putexcel B`i' = matrix(tableA16_`j'T), nformat(number_sep) hcenter vcenter
	local j = `j'+1
}
** R2
forvalues i = 9(7)44 {
	putexcel A`i' = "R-squared", left vcenter
}
** Column numbers
global cells "B C D E F B C D E F B C D E F B C D E F B C D E F B C D E F"
forvalues i = 1(1)30 {

	local y: word `i' of $cells

	if `i' < 6 {
		putexcel `y'5 = "(`i')", hcenter vcenter
	}

	if `i' > 5 & `i' < 11 {
		putexcel `y'12 = "(`i')", hcenter vcenter
	}

	if `i' > 10 & `i' < 16 {
		putexcel `y'19 = "(`i')", hcenter vcenter
	}

	if `i' > 15 & `i' < 21 {
		putexcel `y'26 = "(`i')", hcenter vcenter
	}
	
	if `i' > 20 & `i' < 26 {
		putexcel `y'33 = "(`i')", hcenter vcenter
	}
	
	if `i' > 25 {
		putexcel `y'40 = "(`i')", hcenter vcenter
	}
}
macro drop cells


* -------------------------------- ENDS HERE -------------------------------- *